﻿using Bll;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Model;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace HouseType_API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class HouseController : ControllerBase
    {
        private readonly houseBll bll;

        public HouseController(houseBll bll)
        {
            this.bll = bll;
        }

        /// <summary>
        /// 房型添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult HouseTypeAdd(houseModel info)
        {
            try
            {
                int res = bll.HouseTypeAdd(info);
                return Ok(new
                {
                    data = res
                });

            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 房型显示查询
        /// </summary>
        /// <param name="indexs"></param>
        /// <param name="counts"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult HouseTypeList(int pageindex = 1, int pagesize = 2)
        {
            try
            {
                int indexs = 0;
                int counts = 0;
                var list = bll.HouseTypeList(out indexs, out counts, pageindex, pagesize);
                return Ok(new
                {
                    data = list,
                    indexs = indexs,
                    counts = counts
                });
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 逻辑删除
        /// </summary>
        /// <param name="h_id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DeleteInfo(int h_id)
        {
            try
            {
                var list = bll.DeleteInfo(h_id);
                return Ok(new
                {
                    data = list
                });
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="h_state"></param>
        /// <param name="h_id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult UplowState(int h_state, int h_id)
        {
            try
            {
                var list = bll.UplowState(h_state, h_id);
                return Ok(new
                {
                    data = list
                });
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 导出数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ExportExcel(int pageindex = 1, int pagesize = 2)
        {
            try
            {
                byte[] data = null;
                string contentType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
                string fileName = "序号列表.xlsx";

                //创建一个工作簿
                IWorkbook book = new XSSFWorkbook();

                ///创建一个表
                ISheet sheet = book.CreateSheet();

                //创建第一行
                IRow row1 = sheet.CreateRow(0);

                row1.CreateCell(0).SetCellValue("房间号");
                row1.CreateCell(1).SetCellValue("房型");
                row1.CreateCell(2).SetCellValue("房型价格");
                row1.CreateCell(3).SetCellValue("房型面积");

                int rowIndex = 1;
                int counts = 0;
                int indexs = 0;
                var list = bll.HouseTypeList(out indexs, out counts, pageindex, pagesize);
                foreach (var item in list)
                {
                    row1 = sheet.CreateRow(rowIndex);

                    row1.CreateCell(0).SetCellValue(item.HN_id);
                    row1.CreateCell(1).SetCellValue(item.H_name);
                    row1.CreateCell(2).SetCellValue(item.H_price);
                    row1.CreateCell(3).SetCellValue(item.H_area);
                    rowIndex++;
                }
                using (MemoryStream ms = new MemoryStream())
                {
                    book.Write(ms);
                    data = ms.ToArray();
                }
                return File(data,
                        contentType,
                      fileName);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
